# -*- coding = utf-8 -*-
# @Time : 2022/11/8 10:09
# @Author :Henry L
# @File : Three_process.py
# @Software : PyCharm
from datetime import datetime
from Health_Commission_request import TypeRunSpider
import time
from multiprocessing import Process

# 1.启动时切换IP over
# 2.运行两个进程 over
# 3.超过25分钟自动结束 join(1500) over
# 4.以管理员方式进行重启


def run(ntype, az, sname, log_file_num,type):
    o1 = TypeRunSpider(ntype, az, sname, log_file_num)
    if type == 'hos':
        o1.run()
    else:
        o1.run_doc()


def isduringthattime():
    start_time = datetime.now().replace(hour=0, minute=45, second=0)
    end_time = datetime.now().replace(hour=5, minute=30, second=0)
    now_time = datetime.now()
    if start_time <= now_time <= end_time:
        diff45 = end_time - now_time
        time.sleep(diff45.seconds)

if __name__ == '__main__':
    print("Start Process, the time is : %s" % datetime.now())
    isduringthattime()
    procs = []
    # un.change((597, 723), (736, 722))
    p1 = Process(target=run, args=("s6", 1, "", "1", 'hos'))
    # p2 = Process(target=run, args=("s4", 2, "", "2", 'hos'))
    # p3 = Process(target=run, args=("s4", 3, "", "3", 'hos'))
    # p1 = Process(target=run, args=("s4", 1, "", "1", 'doc'))
    # p2 = Process(target=run, args=("s4", 2, "", "2", 'doc'))
    # p3 = Process(target=run, args=("s4", 3, "", "3", 'doc'))
    p1.daemon = True
    # p2.daemon = True
    # p3.daemon = True
    procs.append(p1)
    # procs.append(p2)
    # procs.append(p3)
    p1.start()
    # p2.start()
    # p3.start()
    TIMEOUT = 3600
    start = time.time()
    while time.time() - start <= TIMEOUT:
        if not any(p.is_alive() for p in procs):
            break
    else:
        for p in procs:
            p.terminate()
            p.join()
        print("time out, killing all processes  the time is : %s" % datetime.now())
